package com.lcxsoft.partnerservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.commonutils.MD5;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.UserClient;
import com.lcxsoft.feign.entity.School;
import com.lcxsoft.partnerservice.entity.PartnerUser;
import com.lcxsoft.partnerservice.mapper.PartnerUserMapper;
import com.lcxsoft.partnerservice.query.PartnerAddQuery;
import com.lcxsoft.partnerservice.query.PartnerQuery;
import com.lcxsoft.partnerservice.service.PartnerUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcxsoft.partnerservice.vo.PartnerVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-01-31
 */
@Service
public class PartnerUserServiceImpl extends ServiceImpl<PartnerUserMapper, PartnerUser> implements PartnerUserService {

    @Resource
    private PartnerUserMapper partnerUserMapper;

    @Resource
    private UserClient userClient;

    @Override
    public R insertPartner(PartnerAddQuery query) {
        PartnerUser partnerUser = new PartnerUser();
        BeanUtil.copyProperties(query, partnerUser);
        //向学校表中添加学校返回学校ID
        String schoolId = userClient.addSchool(query.getSchoolName());
        partnerUser.setSchoolId(schoolId);
        partnerUser.setActiveflag(true);
        partnerUser.setCreatetime(LocalDateTime.now());
        partnerUser.setPartnerUserId(UUID.randomUUID().toString());
        //设置初始密码123456
        partnerUser.setPartnerUserPassword(MD5.encrypt("123456"));
        int insert = partnerUserMapper.insert(partnerUser);
        return insert > 0 ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    @Override
    public R findPartnerList(PartnerQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<PartnerUser> partnerUsers = partnerUserMapper.selectList(null);
        PageInfo<PartnerUser> pageInfo = new PageInfo<>(partnerUsers);
        long total = pageInfo.getTotal();
        List<PartnerUser> list = pageInfo.getList();

        List<PartnerVo> partnerVos = new ArrayList<>();

        for (PartnerUser partnerUser : list) {
            PartnerVo partnerVo = new PartnerVo();
            BeanUtil.copyProperties(partnerUser, partnerVo);
            School school = userClient.querySchoolBySchoolId(partnerUser.getSchoolId());
            partnerVo.setSchoolName(school.getSchoolName());
            partnerVos.add(partnerVo);
        }
        return R.ok().data("partners", partnerVos).data("total", total);
    }

    @Override
    public R doLogin(String phone, String password) {
        PartnerUser partnerUser = partnerUserMapper.selectOne(new LambdaQueryWrapper<PartnerUser>()
                .eq(PartnerUser::getPartnerUserPhone, phone)
                .eq(PartnerUser::getPartnerUserPassword, MD5.encrypt(password)));
        if (partnerUser == null){
            return R.error().message("账号或密码错误");
        }
        return R.ok().data("token", JwtUtils.getJwtToken(partnerUser.getPartnerUserId(), partnerUser.getPartnerUserNickname()));
    }
}
